﻿using Abp.Domain.Entities;
using Abp.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AVISOutBound.SSOServer.Orders
{
    public class OrderBase : Entity
    {
        /// <summary>
        /// 预订时间
        /// </summary>
        public DateTime BookingDate { get; set; }

        /// <summary>
        /// 取车时间
        /// </summary>
        public DateTime PickupDateTime { get; set; }

        /// <summary>
        /// 还车时间
        /// </summary>
        public DateTime ReturnDateTime { get; set; }

        /// <summary>
        /// 租期
        /// </summary>
        public int RentalLength
        {
            get
            {
                if (_RentalLength > 0)
                {
                    return _RentalLength;
                }

                var rentalLength = GetRentalLength();
                if (rentalLength > 0)
                {
                    return rentalLength;
                }
                else
                {
                    throw new UserFriendlyException(string.Format("无法获取租期！{0}，{1}", PickupDateTime, ReturnDateTime));
                }
            }
            set
            {
                _RentalLength = value;
            }
        }
        private int _RentalLength = 0;

        /// <summary>
        /// 选中的车组信息
        /// </summary>
        //public CarGroup CarGroup { get; set; }

        ///// <summary>
        ///// 用户选择的增值服务数据（结构：类型 + 数量，多个用“;”隔开）
        ///// 设置增值服务数据时会同步更新车组信息中的增值服务梳理
        ///// </summary>
        //public string EquipList
        //{
        //    get
        //    {
        //        return GetEquipmentString();
        //    }
        //    set
        //    {
        //        _EquipList = value;
        //        SetEquipmentQuantity(); // 设置增值服务的购买数量
        //    }
        //}
        //private string _EquipList = string.Empty;

        /// <summary>
        /// 用户选择的第三方增值服务（JSON结构： [{"type": "-1","productId": "45","productName": "美国WiFi(含夏威夷)","price": "35"}]）
        /// </summary>
        public string ThirdOrders { get; set; }

        ///// <summary>
        ///// 获取预订单的增值服务数据
        ///// </summary>
        ///// <returns></returns>
        //public string GetEquipmentString()
        //{
        //    if (this == null)
        //    {
        //        return string.Empty;
        //    }

        //    if (!string.IsNullOrWhiteSpace(this._EquipList))
        //    {
        //        return this._EquipList;
        //    }

        //    var sbEquipList = new StringBuilder();
        //    if (this.CarGroup != null)
        //    {
        //        foreach (var equip in this.CarGroup.CarEquipments)
        //        {
        //            if (equip.Quantity > 0)
        //            {
        //                sbEquipList.AppendFormat("{0}={1};", equip.Type, equip.Quantity);
        //            }
        //        }

        //        this._EquipList = sbEquipList.ToString().TrimEnd(';');
        //    }

        //    return this._EquipList;
        //}

        ///// <summary>
        ///// 设置增值服务的购买数量
        ///// </summary>
        ///// <returns></returns>
        //protected void SetEquipmentQuantity()
        //{
        //    if (this == null
        //        || string.IsNullOrWhiteSpace(this._EquipList)
        //        || this.CarGroup == null
        //        || this.CarGroup.CarEquipments == null)
        //    {
        //        return;
        //    }

        //    // 重置所有增值服务的数量为0
        //    this.CarGroup.CarEquipments.ForEach(e => e.Quantity = 0);

        //    // 设置增值服务的数量
        //    foreach (var item in this.EquipList.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
        //    {
        //        var eArray = item.Split('=');
        //        int intQuantity = 0;
        //        if (eArray.Length == 2 && int.TryParse(eArray[1], out intQuantity))
        //        {
        //            var equip = this.CarGroup.CarEquipments.FirstOrDefault(e => e.Type == eArray[0]);
        //            equip.Quantity = intQuantity;
        //        }
        //    }
        //}

        /// <summary>
        /// 根据取还车时间来计算租期
        /// </summary>
        /// <returns>租期</returns>
        protected int GetRentalLength()
        {
            if (this.PickupDateTime > this.ReturnDateTime)
            {
                throw new UserFriendlyException($"租期的开始时间({PickupDateTime})不能大于结束时间({ReturnDateTime})");
            }

            if (this.PickupDateTime.Ticks > 0 && this.ReturnDateTime.Ticks > 0)
            {
                var days = (ReturnDateTime - PickupDateTime).Days;
                if (!(ReturnDateTime.Hour == PickupDateTime.Hour && ReturnDateTime.Minute == PickupDateTime.Minute))
                {
                    days += 1;
                }
                return days < 0 ? 0 : days;
            }
            return 0;
        }
    }
}
